unit oEditorFLManager;

{
	created   26.3.2010
	author   	Miroslav Mintal
	purpose
}

interface

uses
   Windows, ComCtrls, Types, oPhysicalEntity, SysUtils, Dialogs, oManager, oProcessItem,
   OProcessObjectConstants, oPolygon, obstacle, Vector, oFrameFL, oFormFL,
    oCoordinates, oLevel, oFrameScene;

type
  EFLManager = class(Exception)
  end;

  TEditorFLManager = class(TManager)
    private
      aName: string;
      aFrame: TFrameFL;
      aEditorFrame: Tfeditor;
      aEntityId : integer;
      aForm: TformFL;
      procedure setValues(paLevel : TLevel);
      procedure setComponent(paFrame: TFrameFL);
    public
      constructor create(paMaster: TManager);

      function ProcessObject(paItem: TProcessItem):TProcessItem; override;
  end;

implementation

uses
  oProcessObjects;

{ TTreeManager }

function TEditorFLManager.ProcessObject(paItem: TProcessItem): TProcessItem;
var
   suc, ret : Boolean;
   level : TLevel;
   list : TProcessingList;
   value : Tinteger;
   phs : TPhysicalEntity;
   obst : TPolygonObstacle;
   coord : TCoordinates;
   j : integer;
   name : string;
   item : TProcessItem;
   levelSize, cellSize, sectorCount : real;
begin
    ret := false;
    suc := false;
    Result := nil;
    level := nil;
    value := nil;

    if paItem.Data <> nil then
     if paItem.Data is TProcessingLevel then
            level := TProcessingLevel(paItem.Data).Level
     else
      if paItem.Data is TInteger then
        value := TInteger(paItem.Data);

    case paItem.Code of
      PO_EDITOR_FL_NEW_FRM : begin
        aForm := TformFL.Create(nil);
        setComponent(aForm.frame AS TFrameFL);
        aFrame.Manager := Self;
        aForm.TfEditor1.Manager := self;
        setValues(nil);
        aEditorFrame := aForm.TfEditor1;
        aFrame.Editing := false;
        name := 'phys0';
        aFrame.txtName.Enabled := true;
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_UNLOAD_PHYS_LAYER, nil));
        list := TProcessingList.Create;
        list.addParameter(TString.Create(name));
        List.addParameter(TInteger.Create(oLevel.PHYS_LAYER));
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_INSERT_LAYER,
                               List));
        aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ID, TInteger.Create(aEntityId)));
        aEditorFrame.LevelSize := oLevel.DEFAULT_SECTOR_SIZE * oLevel.DEFAULT_ROW_COUNT;
        aForm.init;
        aForm.Show;
      end;

      PO_EDITOR_FL_LOAD_FRM : begin
        aForm := TformFL.Create(nil);
        setComponent(aForm.frame AS TFrameFL);
        aFrame.Manager := Self;
        aForm.TfEditor1.Manager := self;
        aEditorFrame := aForm.TfEditor1;
        aFrame.Editing := true;
        aFrame.txtName.Enabled := false;
        aFrame.txtName.Text := (paItem.Data AS TString).String_;
        item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ACTUAL_LEVEL, TString.Create(aFrame.txtName.Text)));
        if item.Data <> nil then
         begin
          level := TProcessingLevel(item.Data).Level;
          if level <> nil then
           begin
            setValues(level);
            aEditorFrame.LevelSize := level.SizeX;
            aForm.init;
            aMaster.ProcessObject(TProcessItem.create(PO_EDITOR_FL_SET_LEVEL, TProcessingLevel.Create(level)));
            item := aMaster.ProcessObject(TProcessItem.create(PO_SCENE_GET_ID, nil));
            aMaster.ProcessObject(TProcessItem.create(PO_EDITOR_FL_SET_ID, TInteger.Create(TInteger(item.Data).Integer_)));
            aForm.Show;
           end
          else
            raise EFLManager.Create('Error on level load');
         end;
      end;

      PO_EDITOR_FL_CANCEL : BEGIN
         if not aFrame.Editing then
           aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_DELETE_PHYS_LAYER, TString.Create('phys0')));
      END;

      PO_EDITOR_FL_SET_LEVEL: begin
        for phs in level.allObstacles do
        begin
          if phs is TPolygon then
           begin
             obst := TPolygonObstacle.Create;
             for j := 0 to TPolygon(phs).Points.Count - 1 do
             begin
               coord := TCoordinates(TPolygon(phs).Points[j]);
               obst.AddPoint(TVector3f.Create(coord.x, coord.y, 0));
             end;
           obst.ID := phs.ID;
           if phs.LogicalEntity = nil then
             obst.LogEntity:= false
           else obst.LogEntity:= true;
           coord:= TCoordinates(TPolygon(phs).Position);
           obst.Position:= TVector3f.Create(coord.X, coord.Y, 0);
           aEditorFrame.Obstacles.Add(obst);
           aEditorFrame.AllObstacles.Add(obst);
           end;
        end;
      end;

      PO_EDITOR_FL_CHANGE_LEVEL_SIZE : begin
      if aEditorFrame <> nil then
       begin
        aEditorFrame.LevelSize := StrToFloat(aFrame.leLevelSize.Text);
        aForm.init;
       end;
      end;

      PO_EDITOR_FL_SET_ID : begin
        aEditorFrame.aEntityId := value.Integer_;
      end;

      PO_EDITOR_FL_SAVE : begin // treba kontrolovat zmeny a podla toho insert, edit ...
      if aFrame.txtName.Text <> '' then
        begin
        if not aFrame.Editing then
          begin
           list := TProcessingList.Create;
           list.addParameter(TString.Create(aFrame.txtName.Text));

           // setting tab
           with aFrame do
            try
             levelSize := StrToFloat(leLevelSize.Text);
             if levelSize <= 0 then
              raise Exception.Create('Error: negative value');

             sectorCount := StrToFloat(leSectorCount.Text);
             if sectorCount <= 0 then
              raise Exception.Create('Error: negative value');
             cellSize := StrToFloat(leCellSize.text);
             if cellSize <= 0 then
              raise Exception.Create('Error: negative value');

             list.addParameter(TReal.Create(levelSize));
             list.addParameter(TReal.Create(sectorCount));
             list.addParameter(TReal.Create(cellSize));
             except on E : Exception do BEGIN
              raise EFLManager.Create('Error: input values');
              exit;
             END;
            end;
           item := aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_INSERT_PHYS_LAYER, list));
           aMaster.ProcessObject(TProcessItem.create(PO_TREE_INSERT_PHYS_LAYER, TString.Create(aFrame.txtName.Text)));
           aFrame.Success := true;
           if item.Code = PO_FAILURE then
            begin
              aFrame.Success := false;
              ShowMessage('Update not successful');
            end;
          end
        else
         begin
          aMaster.ProcessObject(TProcessItem.create(PO_PROJECT_SAVE_PHYS_LAYER, TString.Create(aFrame.txtName.Text)));
          aFrame.Success := true;
         end;
        end
       else
        ShowMessage('Name not specified');
      end;
      else raise EFLManager.Create('not specified operation for TEditoFLManager : ' + IntToStr(paItem.Code));
    end;

    if (ret) then begin
      if (suc) then
        Result := TProcessItem.create(PO_SUCCESS, nil)
      else
        Result := TProcessItem.create(PO_FAILURE, nil);
    end
    else
      Result := nil;
end;


procedure TEditorFLManager.setComponent(paFrame: TFrameFL);
begin
  aFrame := paFrame;
end;

procedure TEditorFLManager.setValues;
begin
  with aFrame do
   if paLevel = nil then
     begin
       leLevelSize.Text := FloatToStr(oLevel.DEFAULT_SECTOR_SIZE *
          oLevel.DEFAULT_ROW_COUNT);
       leSectorCount.Text := FloatToStr(oLevel.DEFAULT_COLUMN_COUNT);
       leCellSize.Text := FloatToStr(oLevel.DEFAULT_CELL_SIZE);
       leLevelSize.Enabled := true;
       leSectorCount.Enabled := true;
       leCellSize.Enabled := true;
     end
  else begin
       leLevelSize.Text := FloatToStr(paLevel.SizeX);
       leSectorCount.Text := FloatToStr(paLevel.SectorRowCount);
       leCellSize.Text := FloatToStr(paLevel.CellSize);
       leLevelSize.Enabled := false;
       leSectorCount.Enabled := false;
       leCellSize.Enabled := false;
  end;
end;

constructor TEditorFLManager.create(paMaster: TManager);
begin
  inherited Create(paMaster);
  aName := 'none';
//  aFrame := nil;
end;

end.
